package com.aios.action.settings.controller;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.validator.routines.UrlValidator;
import org.apache.log4j.Logger;

import com.aios.entity.unit.ControllerType;
import com.aios.service.ExistValidationEnum;

public class ControllerSaveAction extends AbstractControllerAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ControllerSaveAction.class);
	private int operateMode;

	

	public String execute() {


		validateController();

		if (actionError != null && !actionError.equals("")) {
			return INPUT;
		}

		if (getMode() == Mode.ADD) {

			ExistValidationEnum existValidationEnum = controllerService
					.nameExistValidation(controller.getId(), controller
							.getName(), getSessionUserVariable().getId());

			if (existValidationEnum != ExistValidationEnum.NOT_EXIST) {
				// addActionError(getText("error.name.exist"));
				actionError = getText("error.name.exist");
				return INPUT;
			}

			try {
				controller.setOperateMethod(String.valueOf(operateMode));
				controllerService.save(controller, getSessionUserVariable()
						.getId());
				// addActionMessage(getText("global.success.insert"));
				actionMessage = getText("global.success.insert");
			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.insert");
				return INPUT;
			}

		} else if (getMode() == Mode.EDIT) {

			ExistValidationEnum existValidationEnum = controllerService
					.nameExistValidation(controller.getId(), controller
							.getName(), getSessionUserVariable().getId());

			if (existValidationEnum != ExistValidationEnum.NOT_EXIST) {
				// addActionError(getText("error.name.exist"));
				actionError = getText("error.name.exist");
				return INPUT;
			}

			try {
				
				controller.setOperateMethod(String.valueOf(operateMode));
				controllerService.update(controller, getSessionUserVariable()
						.getId());
				actionMessage = getText("global.success.update");

			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.update");
				return INPUT;
			}
		} else {
			actionError = getText("error.invalid.id");
			return INPUT;
		}

		return SUCCESS;
	}

	private void validateController() {
		if (controller.getName().isEmpty()) {
			actionError = getText("error.name.empty");
		}
		/*if (!controller.getUrl().equals("")) {
			try {
				UrlValidator urlValidator = new UrlValidator();
				if (!urlValidator.isValid(controller.getUrl())) {
					actionError = getText("error.controller.url.invalid");
				}
			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.controller.url.invalid");
			}
		}*/

	}

	public int getOperateMode() {
		return operateMode;
	}

	public void setOperateMode(int operateMode) {
		this.operateMode = operateMode;
	}
	
	

}
